﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using IrrKlang;

namespace AimGameLibrary.GameObject
{
    public class GameIrrKlangPlayer : BaseGameObject
    {
        ISoundEngine engine;
        String FileAudio;

        String PathAudio;
        public String PathAudiO { set { PathAudio = value; } }

        Boolean PlayLoop;

        public GameIrrKlangPlayer(ISoundEngine engine)
        {
            PathAudio = "NULL";
            this.engine = engine;
            //engine.SoundVolume = 1;
        }

        public override void LoadData(Identification getdata, int IndexData, NodeData node)
        {
            if (getdata.Items.GetValue(IndexData + 2).GetType() == typeof(String))
                FileAudio = (String)getdata.Items.GetValue(IndexData + 2);

            if (getdata.Items.GetValue(IndexData + 3).GetType() == typeof(Boolean))
                PlayLoop = (Boolean)getdata.Items.GetValue(IndexData + 3);

            PathAudio = Path.Combine(PathAudio, Script.SubAudio);
            PathAudio = Path.Combine(PathAudio, FileAudio);

        }

        public override void UpdateTime(TimeSpan timespan)
        {
            base.UpdateTime(timespan);
            Logic();
        }

        public override void UpdateGameTime(Microsoft.Xna.Framework.GameTime gametime)
        {
            base.UpdateGameTime(gametime);
            Logic();
        }

        void Logic()
        {
            if (engine != null)
            {
                if (PathAudio != "NULL" && PathAudio != null && File.Exists(PathAudio))
                {
                    if (PlayLoop)
                    {
                        if (!engine.IsCurrentlyPlaying(PathAudio))
                            engine.Play2D(PathAudio, PlayLoop);
                    }
                    else engine.Play2D(PathAudio, PlayLoop);
                }
            }
        }

        public override void UnLoadContent()
        {
            base.UnLoadContent();
            
            engine.RemoveSoundSource(PathAudio);
            engine.RemoveAllSoundSources();
        }
    }
}
